import React from 'react';
import { Form, Radio, Select, InputNumber, Modal } from 'antd';
import { MarkConfig } from '../../components';
import styles from './ExtendRule.less';

const { Option } = Select;
const ScoreForm = ({ form, titleStyle, data }) => {
    const { getFieldDecorator } = form;
    const { taskBusinessExtendScoreRuleVO = {} } = data;
    const {
        reScoreLimitFlag = '',
        reScoreOverdueDays = '',
        scoreConfiguration = [],
        scoreLimitFlag = '',
        scoreOverdueDays = '',
        scoreOverdueType = '',
        scoreOverdueTypeValue = '',
        // scoreUrgeHours = '',
        // scoreUrgeIntervalHours = '',
        scoreUrgeLimitFlag = ''
        // scoreUrgeMaxTimes = ''
    } = taskBusinessExtendScoreRuleVO;
    const limitDecimals = (values = 1) => {
        if (values) {
            return parseInt(values, 10);
        }
        return '';
    };
    const handleRadioChange = (e, type) => {
        Modal.confirm({
            title: `确定切换为${e.target.value ? '限制' : '不限制'}？`,
            onOk() {
                form.setFieldsValue({
                    [type]: e.target.value
                });
            },
            onCancel() {
                form.setFieldsValue({
                    [type]: e.target.value ? 0 : 1
                });
            }
        });
    };

    const renderInput = (paramName, formItemName, initialValue) => (
        <Form.Item>
            {getFieldDecorator(paramName, {
                initialValue:
                    taskBusinessExtendScoreRuleVO[paramName] && taskBusinessExtendScoreRuleVO[paramName] !== ''
                        ? taskBusinessExtendScoreRuleVO[paramName]
                        : initialValue,
                rules: [
                    {
                        required: true,
                        message:
                            paramName === 'scoreUrgeMaxTimes' ? '请输入人工评分提醒次数' : '请输入人工评分自动催办时间'
                    }
                ]
            })(
                <InputNumber
                    min={1}
                    max={99999}
                    style={{ width: 75 }}
                    formatter={limitDecimals}
                    parser={limitDecimals}
                />
            )}
        </Form.Item>
    );
    return (
        <div>
            <div className={styles.headTitle}>评分规则</div>
            <div className={styles.line}></div>
            <div style={titleStyle}>截止时间配置</div>
            <div className={styles.formRow}>
                <Form.Item label="人工评分截止时间" required>
                    {getFieldDecorator('scoreLimitFlag', {
                        initialValue: scoreLimitFlag === '' ? 0 : scoreLimitFlag
                    })(
                        <Radio.Group style={{ width: 160 }} onChange={e => handleRadioChange(e, 'scoreLimitFlag')}>
                            <Radio value={0}>不限制</Radio>
                            <Radio value={1}>限制</Radio>
                        </Radio.Group>
                    )}
                </Form.Item>
                {!!form.getFieldValue('scoreLimitFlag') && (
                    <>
                        从待评分节点的第
                        <Form.Item>
                            {getFieldDecorator('scoreOverdueDays', {
                                initialValue: scoreOverdueDays === '' ? 5 : scoreOverdueDays,
                                rules: [
                                    {
                                        required: true,
                                        message: '请输入人工评分截止时间'
                                    }
                                ]
                            })(
                                <InputNumber
                                    min={0}
                                    max={99999}
                                    style={{ width: 75 }}
                                    formatter={limitDecimals}
                                    parser={limitDecimals}
                                />
                            )}
                        </Form.Item>
                        个自然日后算逾期评分，系统将自动按
                        <Form.Item>
                            {getFieldDecorator('scoreOverdueType', {
                                initialValue: scoreOverdueType === '' ? '0' : scoreOverdueType
                            })(
                                <Select style={{ width: 120 }}>
                                    <Option value="0">星级评分制</Option>
                                </Select>
                            )}
                        </Form.Item>
                        自动评分，默认为
                        <Form.Item>
                            {getFieldDecorator('scoreOverdueTypeValue', {
                                initialValue: scoreOverdueTypeValue === '' ? '3' : scoreOverdueTypeValue
                            })(
                                <Select style={{ width: 100 }}>
                                    <Option value="1">一星</Option>
                                    <Option value="2">二星</Option>
                                    <Option value="3">三星</Option>
                                    <Option value="4">四星</Option>
                                    <Option value="5">五星</Option>
                                </Select>
                            )}
                        </Form.Item>
                    </>
                )}
            </div>
            <div className={styles.formRow}>
                <Form.Item label="截止重评申请时间" required>
                    {getFieldDecorator('reScoreLimitFlag', {
                        initialValue: reScoreLimitFlag === '' ? 0 : reScoreLimitFlag
                    })(
                        <Radio.Group style={{ width: 160 }} onChange={e => handleRadioChange(e, 'reScoreLimitFlag')}>
                            <Radio value={0}>不限制</Radio>
                            <Radio value={1}>限制</Radio>
                        </Radio.Group>
                    )}
                </Form.Item>
                {!!form.getFieldValue('reScoreLimitFlag') && (
                    <>
                        从最后一个评分人评分完成后第
                        <Form.Item>
                            {getFieldDecorator('reScoreOverdueDays', {
                                initialValue: reScoreOverdueDays === '' ? 30 : reScoreOverdueDays,
                                rules: [
                                    {
                                        required: true,
                                        message: '请输入截止重评申请时间'
                                    }
                                ]
                            })(
                                <InputNumber
                                    min={0}
                                    max={99999}
                                    style={{ width: 75 }}
                                    formatter={limitDecimals}
                                    parser={limitDecimals}
                                />
                            )}
                        </Form.Item>
                        个自然日后截止发起重评；
                    </>
                )}
            </div>
            <div style={titleStyle}>自动催办配置</div>
            <div className={styles.formRow}>
                <Form.Item label="人工评分自动催办时间" required>
                    {getFieldDecorator('scoreUrgeLimitFlag', {
                        initialValue: scoreUrgeLimitFlag === '' ? 0 : scoreUrgeLimitFlag
                    })(
                        <Radio.Group style={{ width: 160 }} onChange={e => handleRadioChange(e, 'scoreUrgeLimitFlag')}>
                            <Radio value={0}>不限制</Radio>
                            <Radio value={1}>限制</Radio>
                        </Radio.Group>
                    )}
                </Form.Item>
                {!!form.getFieldValue('scoreUrgeLimitFlag') && (
                    <>
                        从可开始审核节点的第
                        {renderInput('scoreUrgeHours', 'auditAuto', 5)}
                        个小时后开始；每隔
                        {renderInput('scoreUrgeIntervalHours', 'auditAuto', 5)}
                        个小时重复推送提醒
                        {renderInput('scoreUrgeMaxTimes', 'auditAuto', 3)}
                        次后停止提醒；
                    </>
                )}
            </div>
            <div style={titleStyle}>人员配置</div>
            <div className={styles.formRow}>
                <Form.Item label="评分发起人配置" required>
                    {getFieldDecorator('scoreConfiguration', {
                        initialValue: scoreConfiguration || [],
                        rules: [
                            {
                                required: true,
                                validator(_, value, callback) {
                                    let lineLimit = false;
                                    if (value.length > 0) {
                                        lineLimit = true;
                                    }

                                    if (!lineLimit) {
                                        callback('请添加评分发起人配置');
                                    } else {
                                        callback();
                                    }
                                }
                            }
                        ]
                    })(<MarkConfig form={form} />)}
                </Form.Item>
            </div>
        </div>
    );
};
export default ScoreForm;
